package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findPage")
    public List<User>  findPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "10") Integer pageSize){
        return userService.findPage(page,pageSize);
    }


    @GetMapping(value = "/downLoadXlsByJxl",name = "使用jxl导出excel")
    public void downLoadXlsByJxl(HttpServletResponse response) throws Exception {
        //userService.downLoadXlsByJxl(response);
    }

    @PostMapping(value = "/uploadExcel", name = "POI上传用户数据")
    public void uploadExcel(MultipartFile file)  throws Exception{
        //POI用Excel上传用户数据
        userService.uploadExcel(file);
        //EasyPOI用Excel上传用户数据
        userService.uploadExcelWithEasyPOI(file);

    }


    @GetMapping(value = "/downLoadXlsxByPoi", name = "POI导出用户数据")
    public void downLoadXlsxByPoi(HttpServletResponse response)  throws Exception{
        //POI导出用户数据
        userService.downLoadXlsxByPoi(response);
        //POI导出带样式的数据
        userService.downLoadXlsxByPoiWithCellStyle(response);
        //POI根据模板导出数据
        userService.downLoadXlsxByPoiWithTemplate(response);
    }

    @GetMapping(value = "/download", name = "POI导出用户详细数据")
    public void downLoadUserInfoByTemplate(Long id,HttpServletResponse response)  throws Exception{
        //导出用户详细信息--使用模板
        userService.downLoadUserInfoByTemplate(id,response);
        //导出用户详细信息--使用模板引擎
        userService.downLoadUserInfoByTemplate2(id,response);
        //使用EasyPOI，根据模板导出数据
        userService.downLoadUserInfoByTemplateEasyPOI(id,response);
        //下载pdf文件详情
        userService.downLoadUserInfoPDF(id,response);
    }

    /**
     * dom4j:一次性加载xml文件再解析
     * SAX:逐行加载，逐行解析
     *SXSSWorkBook：一旦内存中的对象的个数达到指定值时，就将内存中的这些对象的内容写入到磁盘中，就可以将这些对象从内存中销毁，直至excel导出完成
     *百万数据：不能使用模板，不能使用太多的样式
     */
   @GetMapping(value = "/downLoadMillion", name = "百万数据导出")
   public void downLoadMillion(HttpServletResponse response)  throws Exception{
       //百万数据导出--高版本Excel
       userService.downLoadMillion(response);
   }

    @GetMapping(value = "/downLoadCSV", name = "csv数据导出")
    public void downLoadCSV(HttpServletResponse response)  throws Exception{
       //导出百万数据到CSV文件
       userService.downLoadCSV(response);
        //EasyPOI出百万数据到CSV文件
        userService.downLoadCSVByEasyPOI(response);
    }

    @GetMapping(value = "/{id}", name = "根据id查询用户数据")
    public User findById(@PathVariable("id") Long id)  throws Exception{
        return userService.findById(id);
    }

    @GetMapping(value = "/downloadContract", name = "下载用户的合同")
    public void downloadContract(Long id,HttpServletResponse response)  throws Exception{
        //POI下载用户的合同
        userService.downloadContract(id,response);
        //EasyPOI下载用户的合同
        userService.downloadContractByEasyPOI(id,response);
    }


    @GetMapping(value = "/downLoadWithEasyPOI", name = "EasyPOI方式导出")
    public void downLoadWithEasyPOI(HttpServletResponse response)  throws Exception{
       //使用EasyPOI方式导出
        userService.downLoadWithEasyPOI(response);
    }
    //
    @GetMapping(value = "/downLoadPDF", name = "导出用户数据到PDF中")
    public void downLoadPDF(HttpServletResponse response)  throws Exception{
       //数据从数据库里直接导入到pdf中
       userService.downLoadPDF(response);
       //数据从数据库获取后台处理之后到pdf中
        userService.downLoadPDF2(response);
    }

    //
    @GetMapping(value = "/jfreeChart", name = "jfreeChart图片")
    public void jfreeChart(HttpServletResponse response)  throws Exception{
        userService.jfreeChart(response);
    }
}
